package com.service.atf.reply.service;

import com.service.atf.base.model.Account;
import com.service.atf.base.model.TFTrade;
import com.service.atf.base.utils.ATFLogger;
import com.service.atf.reply.ac.AccountManager;
import com.service.atf.reply.core.IReadExecutor;
import com.luotao.devp.base.utils.StringUtils;

import java.util.Map;

/**
 * User: luotao
 * Date: 12-3-21
 * Time: 17:00:00
 */
public class TradeService1 implements IReadExecutor<TFTrade> {
    @Override
    public boolean isNotify() {
        return true;
    }

    private static final String KEY_BALC_CHARGE = "balcCharge";

    private static final String KEY_BALC_AWARD = "balcAward";

    private static final String KEY_TMP_AWARD = "tmpAward";

    public void initOut(Map out) {
        out.put(KEY_BALC_CHARGE, 0);
        out.put(KEY_BALC_AWARD, 0);
        out.put(KEY_TMP_AWARD, 0);
    }

    @Override
    public void execute(boolean recover, AccountManager accountManager, TFTrade trade, Map out) {
//    System.out.println("trade.getCustomerCode() = "+trade.getCustomerCode());
        if (!recover) {
            ATFLogger.sysdebug("enter trade!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            Account account = accountManager.getBussAccount(trade.getCustomerCode());
//      Validata.validataACNull(this, trade.getCustomerCode(), account);
//      Validata.validataCustomerCode(this, trade.getCustomerCode());
            account.setLastFlowId(trade.getFlowId());
            account.setMd5Bingo(trade.getFlowId(), trade.getAwardMoney());
            account.setFrezCharge(0);
            account.setFrezAward(0);
            accountManager.updateAccount(account);
            out.put(KEY_BALC_CHARGE, account.getBalcCharge());
            out.put(KEY_BALC_AWARD, account.getBalcAward());
            out.put(KEY_TMP_AWARD, account.getTmpAward());

        }//?????
        else {
            ATFLogger.sysdebug("enter trade!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            Account account = accountManager.getBussAccount(trade.getCustomerCode());
            //  Validata.validataACNull(this, trade.getCustomerCode(), account);
            //  Validata.validataCustomerCode(this, trade.getCustomerCode());
            long frezCharge = account.getFrezCharge();
            long frezAward = account.getFrezAward();
            if (frezCharge != 0 || frezAward != 0) {
                account.setFrezCharge(0);
                account.setFrezAward(0);
            } else {
                long chargeTradeMoney = trade.getChargeTradeMoney();
                long awardTradeMoney = trade.getAwardTradeMoney();
                ATFLogger.sysdebug("ATF RECOVER TRADE SHOW TRADE INFO");
                trade.showInfo();
                ATFLogger.sysdebug("ATF RECOVER TRADE SHOW TRADE INFO END");
                if (StringUtils.isEmpty(trade.getMappedFlowId())) {
                    ATFLogger.syserr("??? : ????????");
                    account.setRemainCountSale((short) (account.getRemainCountSale() - 1));
                    account.setRemainSale(account.getRemainSale() - (chargeTradeMoney + awardTradeMoney));
                    account.setBalcAward(account.getBalcAward() - awardTradeMoney);
                    account.setBalcCharge(account.getBalcCharge() - chargeTradeMoney);
                } else {
                    ATFLogger.syserr("??? : ??????????");
                    ATFLogger.sysdebug("??? : ?????????? : " + account.getTmpAward());
                    account.setFrezAward(account.getTmpAward());
                    account.setTmpAward(0);
                }
            }
            account.setLastFlowId(trade.getFlowId());
            account.setMd5Bingo(trade.getFlowId(), trade.getAwardMoney());
            accountManager.updateAccount(account);
        }
    }
}
